package com.iphanta.community.common.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.iphanta.community.user.entity.UserNotification;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.connection.ReactiveSetCommands;

/**
 * @author 墨声 E-mail: zehong.hongframe.huang@gmail.com
 * create time: 2/15/22 7:23 PM
 */
public enum JsonUtils {



    INSTANCE;

    private static final Logger LOGGER = LoggerFactory.getLogger(JsonUtils.class);

    private ObjectMapper mapper;

    JsonUtils() {
        this.mapper = new ObjectMapper();
    }

    public JsonNode toJson(String text) {
        try {
            return mapper.readTree(text);
        } catch (JsonProcessingException e) {
            LOGGER.error("", e);
        }
        return null;
    }

    public String toJsonString(Object o) {
        try {
            return mapper.writeValueAsString(o);
        } catch (JsonProcessingException e) {
            LOGGER.error("", e);
        }
        return null;
    }

    public <T> T toObject(String json, Class<T> tClass) {
        try {
            return mapper.readValue(json, tClass);
        } catch (JsonProcessingException e) {
            LOGGER.error("", e);
        }
        return null;
    }


}
